package com.valuegroup.smarttodo.ui.example;

import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.valuegroup.smarttodo.R;
import com.valuegroup.smarttodo.plugins.fabulousfilter.AAH_FabulousFragment;
import com.valuegroup.smarttodo.plugins.flycotablayout.SlidingCommonTabLayout;
import com.valuegroup.smarttodo.plugins.flycotablayout.TabEntity;
import com.valuegroup.smarttodo.plugins.flycotablayout.listener.CustomTabEntity;
import com.valuegroup.smarttodo.plugins.flycotablayout.listener.OnTabSelectListener;
import com.valuegroup.smarttodo.plugins.flycotablayout.utils.ViewFindUtils;

import java.util.ArrayList;

import androidx.appcompat.app.AppCompatActivity;
import butterknife.BindView;
import butterknife.ButterKnife;

public class ExampleCustomActivity extends AppCompatActivity {


    private String[] goalRange = new String[]{"月度", "季度", "年度"};
    private String[] goalYear = new String[]{"2019年", "2020年", "2021年", "2022年", "2023年"};
    private String[] goalQuarter = new String[]{"1季度", "2季度", "3季度", "4季度"};
    private String[] goalMonth = new String[]{"1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"};

    @BindView(R.id.btnFilterAdd)
    FloatingActionButton btnFilterAdd;

    private ArrayList<CustomTabEntity> goalRangeTabEntities = new ArrayList<>();
    private ArrayList<CustomTabEntity> goalYearTabEntities = new ArrayList<>();
    private ArrayList<CustomTabEntity> goalQuarterTabEntities = new ArrayList<>();
    private ArrayList<CustomTabEntity> goalMonthTabEntities = new ArrayList<>();
    private ExampleFilterFragment goalsFilterFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_example_custom);
        ButterKnife.bind(this);


        goalsFilterFragment = ExampleFilterFragment.newInstance();
        goalsFilterFragment.setParentFab(btnFilterAdd);
        btnFilterAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goalsFilterFragment.setCallbacks(new AAH_FabulousFragment.Callbacks() {
                    @Override
                    public void onResult(Object result) {
                        Log.d("goalFilter", "onResult: " + result.toString());
                        if (result.toString().equalsIgnoreCase("swiped_down")) {
                            //do something or nothing
                            System.out.println("swiped_down");
                        } else {
                            //handle result
                            System.out.println("not swiped_down");
                        }
                    }
                });
                goalsFilterFragment.show(getSupportFragmentManager(), goalsFilterFragment.getTag());
            }
        });


        View decorView = getWindow().getDecorView();

        final SlidingCommonTabLayout btnGoalRange = ViewFindUtils.find(decorView, R.id.t1);
        final SlidingCommonTabLayout btnGoalYear = ViewFindUtils.find(decorView, R.id.t2);
        final SlidingCommonTabLayout btnGoalQuarter = ViewFindUtils.find(decorView, R.id.t3);
        final SlidingCommonTabLayout btnGoalMonth = ViewFindUtils.find(decorView, R.id.t4);

        for (int i = 0; i < goalRange.length; i++) {
            goalRangeTabEntities.add(new TabEntity(goalRange[i], 0, 0));
        }
        for (int i = 0; i < goalQuarter.length; i++) {
            goalQuarterTabEntities.add(new TabEntity(goalQuarter[i], 0, 0));
        }
        for (int i = 0; i < goalMonth.length; i++) {
            goalMonthTabEntities.add(new TabEntity(goalMonth[i], 0, 0));
        }
        for (int i = 0; i < goalYear.length; i++) {
            goalYearTabEntities.add(new TabEntity(goalYear[i], 0, 0));
        }

        btnGoalRange.setTabData(goalRangeTabEntities);
        btnGoalYear.setTabData(goalYearTabEntities);
        btnGoalQuarter.setTabData(goalQuarterTabEntities);
        btnGoalMonth.setTabData(goalMonthTabEntities);

        btnGoalRange.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                btnGoalQuarter.setVisibility(View.VISIBLE);
                btnGoalMonth.setVisibility(View.VISIBLE);

                switch (position) {
                    case 0:
                        btnGoalQuarter.setVisibility(View.GONE);
                        break;
                    case 1:
                        btnGoalMonth.setVisibility(View.GONE);
                        break;
                    case 2:
                        btnGoalQuarter.setVisibility(View.GONE);
                        btnGoalMonth.setVisibility(View.GONE);
                        break;
                }
            }

            @Override
            public void onTabReselect(int position) {

            }
        });

        btnGoalYear.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                reloadData(2, position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });

        btnGoalQuarter.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                reloadData(1, position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });

        btnGoalMonth.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                reloadData(0, position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });


    }

    private void reloadData(int type, int position) {
        String val = "";
        switch (type) {
            case 0:
                val = goalMonth[position];
                break;
            case 1:
                val = goalQuarter[position];
                break;
            case 2:
                val = goalYear[position];
                break;
        }

        Toast t = Toast.makeText(ExampleCustomActivity.this, "类型是" + goalRange[type] + " 值是：" + val, Toast.LENGTH_LONG);
        t.show();
    }


    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (goalsFilterFragment.isAdded()) {
            goalsFilterFragment.dismiss();
            goalsFilterFragment.show(getSupportFragmentManager(), goalsFilterFragment.getTag());
        }
    }


}
